/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var ADS_Client = new Class({
    
    Implements: [Options],
    
    options: {
        url : '',
        clientKey : ''
    },
    //client version
    version : '0.0.2',
    //descriptor revision
    revision : 0,
    //presentation controler
    presentation: null,
    
    initialize: function(options){
        this.setOptions(options);
    },
    
    run: function(){
        var request = new Request.JSON({
            url : this.options.url,
            onSuccess : function(descriptor, responseText){
                this.revision = descriptor.head.revision;
                this.build(descriptor);                
            }.bind(this)
        }).get({
            'do'   : 'sendDescriptor', 
            'ckey' : this.options.clientKey, 
            'sw'   : this.version
        });       
        (function(){var request = new Request.JSON({
            url : this.options.url,
            onSuccess : function(responseJSON, responseText){  
                request.removeEvents('success');
                request.get({
                    'do'   : 'confirmCommands', 
                    'ckey' : this.options.clientKey, 
                    'sw'   : this.version
                });
                //console.log(responseJSON);                
            }.bind(this)
        }).get({
            'do'   : 'sendCommands', 
            'ckey' : this.options.clientKey, 
            'sw'   : this.version
        })}).periodical(60000,this);    
    
        return this;
        
    },
    
    build: function(descriptor){  
        //load data sources
        var dataSourceController = new DataSourceController();
        Array.each(descriptor.datasources, function(nodeSource, key){
            var dataSource = new DataSource({
                'id'      : nodeSource.id,
                'content' : nodeSource.content
            });
            //add items to datasource
            Array.each(nodeSource.timeline, function(nodeItem, key){
                var item = new Item({
                    'url'       : nodeItem.url,
                    'type'      : nodeSource.content,
                    'duration'  : nodeItem.duration,
                    'validFrom' : nodeItem.validFrom,
                    'validTo'   : nodeItem.validTo,
                    'mime'      : nodeItem.mime
                });
                dataSource.addItem(item);
            });
            dataSourceController.addSource(dataSource);
        });
        //load data frames
        var dataFrameController = new DataFrameController();
        Array.each(descriptor.frames.data, function(nodeFrame, key){
            var dataFrame = new DataFrame({
                'id'     : nodeFrame.id,
                'htmlId' : nodeFrame.htmlId
            });
            //add items to data frame
            Array.each(nodeFrame.timeline, function(nodeItem, key){
                //console.log(nodeFrame.htmlId+'-'+nodeItem.id);
                dataFrame.addSource(dataSourceController.find(nodeItem.id));
            });
            dataFrameController.addFrame(dataFrame);
        });
        //load slides
        var slideController = new SlideController();       
        Array.each(descriptor.frames.slides, function(nodeSlide, key){
            var slide = new Slide({
                'id'  : nodeSlide.id,
                'url' : nodeSlide.url
            });
            //add data frame to slide            
            Array.each(nodeSlide.zones, function(zone, key){
                slide.addFrame(zone.htmlId, dataFrameController.find(zone.frameId));
            });
            slideController.addSlide(slide);
        });
        //start presentation
        this.presentation = new PresentationController({
            'sc'  : slideController,
            'dfc' : dataFrameController
        });
        this.presentation.addEvent('checkDescriptor', function(){
            new Request.JSON({
                url : this.options.url,
                onSuccess : function(responseJSON, responseText){
                    if(this.revision != responseJSON.revision){
                        window.location.reload(true);
                    }else{
                        console.log('Descriptor is up to date.')
                    }           
                }.bind(this)
            }).get({
                'do'       : 'sendCurrentRevision', 
                'ckey'     : this.options.clientKey, 
                'sw'       : this.version
            });
        }.bind(this));
        this.presentation.start();
        
    },
    
    getPresentation : function(){
        return this.presentation;
    }
    
});